package com.ellecity06.cwhiskymall.adapter

import android.content.Context
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ellecity06.common.ktx.colorResToColorId
import com.ellecity06.common.ktx.dp2px
import com.ellecity06.common.ktx.load
import com.ellecity06.common.viewholder.ViewBindingHolder
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.bean.response.GoodResponse
import com.ellecity06.cwhiskymall.databinding.ItemClassifyRightBinding
import com.ellecity06.cwhiskymall.storage.UserStorage
import com.ellecity06.cwhiskymall.view.span.RadiusBackgroundSpan
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * @author joe
 * @time
 * @des 分类顶部
 */
class ClassifyRightAdapter :
    BaseQuickAdapter<GoodResponse, ViewBindingHolder<ItemClassifyRightBinding>>() {

    override fun onBindViewHolder(
        holder: ViewBindingHolder<ItemClassifyRightBinding>,
        position: Int,
        item: GoodResponse?
    ) {
        holder.binding.tvGoodsName.text =
            if (item?.goodsSource == 0) setGoodName(item) else item?.spuName
        holder.binding.ivGoodImg.load(context, item?.mainImage, 10F)
        holder.binding.tvPrice.text = "￥${item?.salesPrice}丨经典品质"
        var contribution = "0.00"
        if (!item?.contribution.isNullOrEmpty()) {
            val con = item!!.contribution!!.toDouble()
            contribution = if (con >= 10000) {
                BigDecimal(con / 10000.0).setScale(2, RoundingMode.DOWN).toString()+"w"
            } else {
                item.contribution!!
            }
        }
        if (UserStorage.isHasUp()){
            holder.binding.tvGongJin.text = buildSpannedString {
                scale(0.55F) {
                    append("赠送")
                }
                append(contribution)
                scale(0.55F) {
                    append("贡献金")
                }
            }
        }else {
            holder.binding.tvGongJin.text = "¥${item?.salesPrice}"
        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): ViewBindingHolder<ItemClassifyRightBinding> {
        val view = ItemClassifyRightBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewBindingHolder(view)
    }

    private fun setGoodName(item: GoodResponse?): SpannableStringBuilder {
        val tag = "自营"
        val goodName = tag + " " + item?.spuName
        val builder = SpannableStringBuilder(goodName)
        builder.setSpan(
            RadiusBackgroundSpan(R.color.colorAccent.colorResToColorId(), 4),
            0,
            tag.length,
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
        // 改变字体大小
        builder.setSpan(
            AbsoluteSizeSpan(17f.dp2px()),
            tag.length,
            goodName.length,
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
        builder.setSpan(
            StyleSpan(Typeface.BOLD),
            tag.length,
            goodName.length,
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
        return builder
    }

}